﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Infrastructure.ViewModel;
using Microsoft.Practices.Prism.Regions;
using UltimateNavigationFramework.UriNavigation;

namespace Ultimate.Shell.ViewModels
{
    public class ErrorViewModel : ViewModelBase, INavigationAware, IPageTitle
    {
        #region Private members
        private readonly UltimateNavigationFrameLoadingService _frameLoaderService;
        #endregion

        #region Requested Uri Property
        private string _requestedUri;
        public String RequestedUri
        {
            get { return _requestedUri; }
            set
            {
                if (_requestedUri != value)
                {
                    _requestedUri = value;
                    this.NotifyPropertyChanged(() => this.RequestedUri);
                }
            }
        }
        #endregion

        #region Constructor
        public ErrorViewModel(IUltimateNavigationFrameLoadingService frameLoaderService)
        {
            this._frameLoaderService = frameLoaderService as UltimateNavigationFrameLoadingService;
        }
        #endregion

        #region INavigationAware Implementation
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var exception = navigationContext.Parameters["exception"];
            var stackTrace = navigationContext.Parameters["stackTrace"];
            var innerException = navigationContext.Parameters["innerException"];

            var originalString = navigationContext.Uri.OriginalString;
            if (!string.IsNullOrEmpty(originalString))
            {
                var queryIndex = originalString.IndexOf('?');
                this.RequestedUri = originalString.Remove(queryIndex, originalString.Length - queryIndex);
            }
        }
        #endregion

        #region IPageTitle Implementation
        public string PageTitle
        {
            get { return "Page Not Found"; }
        }
        #endregion
    }
}
